package mysql

import (
	"gorm.io/gorm"
	"video-manager/usersrpc/usersrpc"
)

type Topic struct {
	gorm.Model
	Topic_name  string `gorm:"type:varchar(50);not null;commit:'话题名称'"`
	Topic_intro string `gorm:"type:text;commit:'话题描述'"`
	Hot_level   int    `gorm:"type:int(11);default:0;commit:'话题热度'"`
	Create_time string `gorm:"type:varchar(50);commit:'创建时间'"`
}

func (t *Topic) FindTopic(db *gorm.DB, name string) error {
	return db.Debug().Where("topics.topic_name=?", name).FirstOrCreate(t).Error
}

func TopicList(db *gorm.DB, list []*usersrpc.TopicList) []*usersrpc.TopicList {
	/*
		SELECT
		  topics.topic_name AS topic_name,
		  videos.video_title AS video_title,
		  videos.cover_link AS cover_link
		FROM
		  topics
		  JOIN videos ON videos.topic_id = topics.id
	*/
	db.Debug().Model(Topic{}).Select("topics.topic_name AS topic_name",
		"videos.video_title AS video_title",
		"videos.cover_link AS cover_link").
		Joins("JOIN videos ON videos.topic_id = topics.id").
		Order("play_num DESC").Limit(3).
		Find(&list)
	return list
}
